using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantSubmarineLastLifeAnimationController
/// \brief  The animation controller of the submarine last life
[AddComponentMenu("GK/AbyssalGiant/Submarine/gkAbyssalGiantSubmarineLastLifeAnimationController")]
public class gkAbyssalGiantSubmarineLastLifeAnimationController : MonoBehaviour 
{
	/// The life component
	private gkLifeComponent m_rLifeComponent;
	
	/// The appear on event component
	private gkAppearOnEvent m_rAppearOnEvent;
	
	/// \brief  Called just after the game begin to play
    void Start()
    {
		// Get the appear on event component
		m_rAppearOnEvent = GetComponent<gkAppearOnEvent>();
	}
	
	/// \brief  Called when the component is enabled
	void OnEnable() 
	{
		// Grab the life component of the hierarchy the life is on
		m_rLifeComponent = transform.root.GetComponentInChildren<gkLifeComponent>();
		
		// Listen the shock event of the owner
		m_rLifeComponent.GetComponent<gkEventComponent>().AddEventHandler(gkAbyssalGiantLifeEvent.LastLife, HandleLastLife);
	}
	
	/// \brief  Called when the component is disabled
	void OnDisable() 
	{	
		// Remove the shock event of the owner
		m_rLifeComponent.GetComponent<gkEventComponent>().RemoveEventHandler(gkAbyssalGiantLifeEvent.LastLife, HandleLastLife);
	}
	
	/// \brief  Handle the life change
	void HandleLastLife(Object m_rEvent)
	{
		animation.Play("Life_LastOne");
		m_rAppearOnEvent.enabled = false;
	}
}